﻿using Dapper_Console.IRepository;
using Dapper_Console.IServices;
using Dapper_Console.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;

namespace Dapper_Console.Services
{
    public class baseServices<T> : IbaseServices<T> where T :BaseEntity
    {
        
        /// <summary>
        /// 依赖注入
        /// </summary>
        private readonly IBaseRepository<T> _baseRepository;   

        public baseServices(IBaseRepository<T> baseRepository)
        {
            _baseRepository = baseRepository;
        }

      


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public virtual async Task<bool> Insert(T t)
        {
            return await _baseRepository.Insert(t);
        }
      
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public virtual async Task<bool> Update(T t)
        {
            return await this._baseRepository.Update(t);
        }
        /// <summary>
        /// 删除单条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual async Task<bool> Delete(long id)
        {
            return await this._baseRepository.Delete(id);
        }
        

        /// <summary>
        /// 获取单条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual async Task<T> Get(long id)
        {
            return await _baseRepository.GetById(id);
        }

        public virtual async Task<IEnumerable<T>> GetList()
        {
            return await this._baseRepository.GetList();
        }

        public virtual async Task<IEnumerable<T>> GetPagedList(int pageNumber, int pageSize, string conditions, string orderby, object param)
        {
            return await this._baseRepository.GetPagedList(pageNumber, pageSize, conditions, orderby, param);
        }

        public virtual async Task<int> RecordCount(string conditions, object param)
        {
            return await this._baseRepository.RecordCount(conditions, param);
        }

        public virtual async Task<IEnumerable<T>> Query(string sql, object param)
        {
            return await this._baseRepository.Query(sql, param);
        }
        /// <summary>
        /// 执行SQL
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public virtual async Task<int> Execute(string sql, object param)
        {
            return await this._baseRepository.Execute(sql, param);
        }

        public virtual async Task<int> ExecuteScalar(string sql, object param)
        {
            return await this._baseRepository.ExecuteScalar(sql, param);
        }
        public virtual async Task<int> ExecuteWithTransactionAsync(string sql, object param) {
            return await _baseRepository.ExecuteWithTransactionAsync(sql, param);
        }

        public virtual async Task<T> QueryFirst(string sql, object param)
        {
            return await this._baseRepository.QueryFirst(sql, param);
        }

        public virtual async Task<int> GetMaxId(string tableName)
        {
            return await this._baseRepository.ExecuteScalar("SELECT max(id) from " + tableName, null);
        }

        public virtual int ExecuteWithTransaction(string sql, object param) {
            return this._baseRepository.ExecuteWithTransaction(sql, param);
        }

    }
}
